package com.study.controller;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.domain.User;
//import com.study.domain.User2;
//import com.study.mapper.User2Mapper;
import com.study.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class UserListController {
    @Autowired
    UserMapper userMapper;

    //@Autowired
    //User2Mapper user2Mapper;

    //@RequestMapping("/listall")
    @RequestMapping("/login")
    public String listCategory(Model m,
                               @RequestParam(value = "start", defaultValue = "0")
                                       int start,
                               @RequestParam(value = "size", defaultValue = "5")
                                       int size,
                               HttpServletRequest request
    ) throws Exception {
      //1. 在参数里接受当前是第几页 start ，以及每页显示多少条数据 size。 默认值分别是0和5。
        //2. 根据start,size进行分页，并且设置id 倒排序
        PageHelper.startPage(start, size, "id desc");
        //3. 因为PageHelper的作用，这里就会返回当前分页的集合了
        List<User> cs = userMapper.queryAll();
        //4. 根据返回的集合，创建PageInfo对象
        PageInfo<User> page = new PageInfo<>(cs);
        //5. 把PageInfo对象扔进model，以供后续显示
        m.addAttribute("page", page);
        //System.out.println(page..is.end.e.isIsLastPage().I.getLastPage().f.isIsFirstPage().getFirstPage().getLastPage().isIsFirstPage());
        //6. 跳转到listCategory.jsp
        //return "list";

        System.out.println("Session().getAttribute=>"+request.getSession().getAttribute("username"));
 /*
        //判断是否已经登录,如果session中用户名为空，进入登录
        if (request.getSession().getAttribute("username") == null) {
            System.out.println("login进入");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            System.out.println("username=>" + username);
            *//*Todo 从这里开始去连接数据库校验*//*
            User2 user2 = user2Mapper.findByNameAndPassword(username, password);
            System.out.println("user2=>" + user2);
            if (user2 == null) {
                //    登录失败，给用户提示
                String msg = "用户名或者密码错误，请重新输入";
                m.addAttribute("msg", msg);
                return "login";
            } else {
                //   成功就跳转
                m.addAttribute("username", username);
                //并且在session中也添加用户名
                request.getSession().setAttribute("username",username);
                request.getSession().setMaxInactiveInterval(10);//页面10秒没操作，就session失效
                return "list";
            }
        } else {
            return "list";
        }
*/
            return "list";
    }
}
